﻿using System;
using System.Web.Security;
using System.Web.UI;
using THANHTUNGO.Portal.Components.Security;

namespace THANHTUNGO.Portal.Controls
{
    public partial class SignIn : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSignIn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                // Attempt to Validate User Credentials using User
                IdentityUser identityUser = User.Login(txtEmail.Text, txtPassword.Text);

                if (identityUser != null)
                {
                    // Use security system to set the UserID within a client-side Cookie
                    FormsAuthentication.SetAuthCookie(identityUser.ToString(), chkRemember.Checked);

                    // Redirect browser back to recent page.
                    if (Request.QueryString["returnUrl"] != null)
                    {
                        int pos = Request.RawUrl.ToLower().IndexOf("returnurl");
                        string returnUrl = Request.RawUrl.Substring(pos + 10);
                        Response.Redirect(Server.UrlDecode(returnUrl));
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
                else
                {
                    txtMessage.Text = "Đăng nhập không thành công!";
                }
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Register.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}